
package com.lzy.business.controller;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping(value = "admin")
public class LoginController {
    private static final Logger LOGGER = LoggerFactory.getLogger(LoginController.class);

    /**
     * 默认页面
     *
     * @return
     */
    @RequestMapping(value = "login", method = RequestMethod.GET)
    public String login() {
        Subject subject = SecurityUtils.getSubject();
        if (subject.isAuthenticated() || subject.isRemembered()) {
            return "redirect:/admin/index";
        }
        return "admin/login";
    }

    /**
     * 登录失败
     *
     * @param model
     * @return
     */
    @RequestMapping(value = "login", method = RequestMethod.POST)
    public String fail(HttpServletRequest request, Model model) {
        if (request.getAttribute("shiroLoginFailure") == null) {
            return "redirect:/admin/index";
        }
        String message = ((String) request.getAttribute("shiroLoginFailure"));
        if (message != null && message.contains("com.lzy.product.common.exception.")) {
            message = message.replace("com.lzy.product.common.exception.", "");
        }
        if (message != null && message.contains("org.apache.shiro.authc.")) {
            message = message.replace("org.apache.shiro.authc.", "");
        }
        switch (message) {
            case "IncorrectCredentialsException":
                model.addAttribute("msg", "密码错误");
                break;
            case "UnknownAccountException":
                model.addAttribute("msg", "账号不存在");
                break;
            case "LockedAccountException":
                model.addAttribute("msg", "账号被锁定，请15分钟后重试");
                break;
            case "AccountFrozenException":
                model.addAttribute("msg", "账号被锁定，请联系管理员");
                break;
            case "CaptchaException":
                model.addAttribute("msg", "验证码错误");
                break;
            case "PermissionException":
                model.addAttribute("msg", "密码不能为空");
                break;
            case "AuthenticationException":
                model.addAttribute("msg", "验证码错误");
                break;
            case "UnderReviewException":
                model.addAttribute("msg", "审核中");
                model.addAttribute("phone", message);
                return "admin/tenants_success";
            case "AuditFailureException":
                model.addAttribute("msg", "审核失败!");
                model.addAttribute("phone", message);
                return "redirect:/merchant/error";
        }
        return "admin/login";
    }

}

